package com.jiangnan.filter;

import jakarta.servlet.*;
import jakarta.servlet.annotation.WebFilter;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;

@Slf4j
//@WebFilter(urlPatterns = "/*")
public class DemoFilter implements Filter {


    /**
     * 初始化过滤器,web服务器启动时调用一次
     * @param filterConfig
     * @throws ServletException
     */
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        log.info("init 过滤器初始化 .... ");
    }

    /**
     * 拦截到请求之后执行，在业务代码执行之前执行，会执行多次
     * @param servletRequest
     * @param servletResponse
     * @param filterChain
     * @throws IOException
     * @throws ServletException
     */
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        log.info("doFilter 拦截到了请求 ....  放行前的逻辑 ...");
        filterChain.doFilter(servletRequest, servletResponse);
        log.info("doFilter 拦截到了请求 ....  放行后的逻辑 ...");
    }

    /**
     * 销毁过滤器,web服务器关闭时调用一次
     */
    @Override
    public void destroy() {
        log.info("destroy 过滤器销毁 .... ");
    }
}
